#define WIN32_LEAN_AND_MEAN

#include <windows.h>
#include <dinput.h>

#include <vector>
#include <algorithm>

using namespace std;

vector<LPCDIDEVICEINSTANCE> g_joysticks;

BOOL CALLBACK EnumJoysticksCallback(LPCDIDEVICEINSTANCE lpddi, LPVOID pvRef);

int main(char argc, char** argv)
{
	// HINSTANCE  g_hinst; // initialized earlier
	HRESULT         hr;
	LPDIRECTINPUT8  g_lpDI;

	HINSTANCE g_hinst = GetModuleHandle(0);

	hr = DirectInput8Create(g_hinst, DIRECTINPUT_VERSION,
	        IID_IDirectInput8, (void**)&g_lpDI, NULL);
	if FAILED(hr)
	{
	        // DirectInput not available; take appropriate action
		MessageBox(NULL, "error", "no dinput 8", MB_OK);
		return 0;
	}
	MessageBox(NULL, "Message", "dinput 8 present", MB_OK);

	//g_pDI is an initialized pointer to IDirectInput8
	g_lpDI->EnumDevices(DI8DEVCLASS_GAMECTRL, EnumJoysticksCallback,
	                       NULL, DIEDFL_ATTACHEDONLY);
	if (g_joysticks.size() <= 0)
	{
		MessageBox(NULL, "error", "no any joystick in system.", MB_OK);
	} else
	{
		MessageBox(NULL, "Message", "find some joystick in system.", MB_OK);
	}

	return 0;
}

BOOL EnumJoysticksCallback(LPCDIDEVICEINSTANCE lpddi, LPVOID /*pvRef*/)
{
	if (g_joysticks.end() == find(g_joysticks.begin(), g_joysticks.end(), lpddi))
	{
		g_joysticks.push_back(lpddi);
		return DIENUM_CONTINUE;
	}
	return DIENUM_STOP;
}
